package com.yupi.oj.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.oj.annotation.AuthCheck;
import com.yupi.oj.common.BaseResponse;
import com.yupi.oj.common.DeleteRequest;
import com.yupi.oj.common.ErrorCode;
import com.yupi.oj.common.ResultUtils;
import com.yupi.oj.constant.UserConstant;
import com.yupi.oj.exception.BusinessException;
import com.yupi.oj.exception.ThrowUtils;
import com.yupi.oj.model.dto.voucher.VoucherAddRequest;
import com.yupi.oj.model.dto.voucher.VoucherQueryRequest;
import com.yupi.oj.model.dto.voucher.VoucherUpdateRequest;
import com.yupi.oj.model.entity.Voucher;
import com.yupi.oj.service.VoucherService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/voucher")
public class VoucherController {

    @Resource
    private VoucherService voucherService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 新增优惠券
     * @param voucherAddRequest 优惠券新增响应信息
     * @return 优惠券id
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addVoucher(@RequestBody VoucherAddRequest voucherAddRequest) {
        if (voucherAddRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Voucher voucher = new Voucher();
        BeanUtil.copyProperties(voucherAddRequest,voucher);
        voucherService.validVoucher(voucher,true);
        boolean save = voucherService.save(voucher);
        ThrowUtils.throwIf(!save,ErrorCode.OPERATION_ERROR);
        stringRedisTemplate.opsForValue().set("voucher:stock:" + voucher.getId(), voucher.getStock().toString());
        return ResultUtils.success(voucher.getId());
    }

    /**
     * 更新优惠券
     * @param voucherUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateVoucher(@RequestBody VoucherUpdateRequest voucherUpdateRequest){
        if (voucherUpdateRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Voucher voucher = new Voucher();
        BeanUtil.copyProperties(voucherUpdateRequest,voucher);
        voucherService.validVoucher(voucher,false);
        redisTemplate.delete("voucher:stock:"+voucher.getId());
        boolean result = voucherService.updateById(voucher);
        //redisTemplate.opsForValue().set("voucher:stock:"+voucher.getId(),voucher.getStock().toString());
        return ResultUtils.success(result);
    }

    /**
     * 查询优惠券列表
     * @return 优惠券列表
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<Voucher>> queryVoucher(@RequestBody VoucherQueryRequest voucherQueryRequest) {
        int current = voucherQueryRequest.getCurrent();
        int size = voucherQueryRequest.getPageSize();
        Page<Voucher> voucherPage = voucherService.page(new Page<>(current, size), voucherService.getQueryWrapper(voucherQueryRequest));
        return ResultUtils.success(voucherPage);
    }
}
